from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
from common.libs.UrlManager import UrlManager
import os


class Application(Flask):
    def __init__(self, import_name, template_folder=None, root_path=None, static_folder=None):
        super(Application, self).__init__(import_name, template_folder=template_folder, root_path=root_path, static_folder=static_folder)
        self.config.from_pyfile('config/base_setting.py')
        if "os_config" in os.environ:
            self.config.from_pyfile('config/%s_setting.py' % os.environ['os_config'])

        db.init_app(self)


db = SQLAlchemy()
app = Application(__name__, template_folder=os.getcwd() + "/web/templates/", root_path=os.getcwd(), static_folder=os.getcwd()+"/web/")
manage = Manager(app)


'''
函数模板
装饰器直接将函数注册为模板全局函数
其中第一个参数是自定义的全局函数 ，第二个参数是自定义的全局函数的名字
'''


app.add_template_global(UrlManager.buildStaticUrl, 'buildStaticUrl')
app.add_template_global(UrlManager.buildUrl, 'buildUrl')
